/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package teste;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 *
 * @author MEGADETH
 */
public class filehandling {

    	private File file;
	private FileInputStream fis;
	private FileOutputStream fos;

	private DataInputStream dis;
	private DataOutputStream dos;

	public filehandling()
	{
		file = new File("d:/Desert.jpg");
	}

	public static void main(String args[])
	{
		filehandling app = new filehandling();

		app.copyFile1();
		System.out.println("Done copy file 1...");

		app.copyFile2();
		System.out.println("Done copy file 2...");

		app.copyFile3();
		System.out.println("Done copy file 3...");

		try{
			app.dis.close();
			app.fis.close();
		}
		catch(IOException ioe){
			ioe.printStackTrace();
		}
	}

	// copy file first way
	public void copyFile1()
	{
		openFileInput();

		try{
			fos = new FileOutputStream("D:/teste/Desert1.jpg");
			dos = new DataOutputStream(new BufferedOutputStream(fos));

			byte b;
			try{
				while(true){
					b = dis.readByte();

					dos.write(b);
				}

			}
			catch(EOFException e){
				System.err.println("Finish read end of file...");
			}

		}
		catch(IOException e){
			System.err.println("IOException...1");
		}
		finally{
			closeOutputStream(dos);
			closeOutputStream(fos);
		}
	}

	// copy file second way
	public void copyFile2()
	{
		openFileInput();

		try{
			fos = new FileOutputStream("D:/teste/Desert2.jpg");
			dos = new DataOutputStream(new BufferedOutputStream(fos));

			byte[] b = new byte[(int) file.length()];

			for(int i=0; i<b.length-1; i++)
			{
				b[i] = dis.readByte();
			}

			dos.write(b);
		}
		catch(IOException e){
			System.err.println("IOException...2");
			e.printStackTrace();
		}
		finally{
			closeOutputStream(dos);
			closeOutputStream(fos);
		}
	}

	// copy file third way
	public void copyFile3()
	{
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		byte[] b = new byte[200];

		openFileInput();

		try{
			fos = new FileOutputStream("D:/teste/Desert3.jpg");
			dos = new DataOutputStream(new BufferedOutputStream(fos));

			while(true){
				int length = dis.read(b);

				System.out.println("read : " + length);

				if (length < 0) {
			        break;
				}

				if(length > 0){
					baos.write(b, 0, length);
				}

			}

			dos.write(baos.toByteArray());
		}
		catch(IOException e){
			System.err.println("IOException...3");
		}
		finally{
			closeOutputStream(dos);
			closeOutputStream(fos);
		}
	}

	// open file for input
	public void openFileInput()
	{
		try{
			fis = new FileInputStream(file);
			dis = new DataInputStream(new BufferedInputStream(fis));
		}
		catch(FileNotFoundException e){
			System.err.println("File Not Found...!");
		}
	}

	// close output stream
	public void closeOutputStream(OutputStream os)
	{
		try{
			os.close();
		}
		catch(IOException ioe){
			System.err.println("Exception closing output stream");
		}

	}
    
}
